package com.util;

import org.apache.commons.lang3.StringUtils;


import java.util.Arrays;

/**
 * @author seven
 */
public  class AccessFlagsUtil {
    /**
     * 获取类访问修饰符
     * @param accessCode
     * @return
     */
    public static String getClassAccessFlags(String accessCode){
        int[] access = getArray(accessCode);
        StringBuffer result = new StringBuffer();
        if (access[SystemNumber.THREE] == SystemNumber.ONE) {
            result.append("public ");
        }
        if (access[SystemNumber.ONE] == SystemNumber.FOUR) {
            result.append("abstract class");
        }
        if (access[SystemNumber.ONE] == SystemNumber.SIX && access[SystemNumber.ZERO] ==0) {
            result.append("interface ");
        }
        if (access[SystemNumber.ZERO] == SystemNumber.TWO) {
            result.append("@interface ");
        }
        if (access[SystemNumber.ZERO] == SystemNumber.FOUR) {
            result.append("enum ");
        }
        if(result.toString().equals("public ")){
            result.append("class ");
        }
        return result.toString();
    }

    /**
     * 属性访问修饰符
     * @param accessCode
     * @return
     */
    public static String getFieldAccessFlags(String accessCode){
        int[] access = getArray(accessCode);
        StringBuffer result = new StringBuffer();
        if (access[SystemNumber.THREE] == SystemNumber.ONE) {
            result.append("public ");
        }
        if (access[SystemNumber.THREE] == SystemNumber.TWO) {
            result.append("private ");
        }
        if (access[SystemNumber.THREE] == SystemNumber.FOUR) {
            result.append("protected ");
        }
        if (access[SystemNumber.THREE] == SystemNumber.EIGHT) {
            result.append("static ");
        }
        if(access[SystemNumber.THREE] == SystemNumber.NINE){
            result.append("public static ");
        }
        if(access[SystemNumber.THREE] == SystemNumber.TEN){
            result.append("private static ");
        }
        if(access[SystemNumber.THREE] == SystemNumber.TWELVE){
            result.append("protected static ");
        }
        if (access[SystemNumber.TWO] == SystemNumber.ONE) {
            result.append("final ");
        }
        if (access[SystemNumber.TWO] == SystemNumber.FOUR) {
            result.append("volatile ");
        }
        if (access[SystemNumber.TWO] == SystemNumber.EIGHT) {
            result.append("transient ");
        }
        if(access[SystemNumber.TWO] == SystemNumber.NINE){
            result.append("final transient ");
        }
        if(access[SystemNumber.TWO] == SystemNumber.TWELVE){
            result.append("volatile transient ");
        }

        if (access[SystemNumber.ZERO] == SystemNumber.FOUR) {
            result.append("enum ");
        }
        return result.toString();
    }

    /**
     * 获取数组
     * @param accessCode
     * @return
     */
    private static int[] getArray(String accessCode){
        if(StringUtils.isEmpty(accessCode)){
            throw  new RuntimeException("类访问修饰符编码异常");
        }
        int[] access = Arrays.stream(accessCode.split("")).mapToInt(e->{
            try {
               return Integer.valueOf(e);
            }catch (Exception ex){
                return sixteenToTen(e);
            }
        }).toArray();
        if(access.length < SystemNumber.THREE){
            throw  new RuntimeException("类访问修饰符编码异常");
        }
        return access;
    }

    /**
     * 将字符作为16进制转换为10进制数
     * @param sixteen
     * @return
     */
    private static int sixteenToTen(String sixteen){
        switch (sixteen){
            case "A":
                return 10;
            case "B":
                return 11;
            case "C":
                return 12;
            case "D":
                return 13;
            case "E":
                return 14;
            default :
                return 15;
        }
    }
}
